﻿using System;
using System.Windows.Graphics;
using System.Windows.Controls;
using Microsoft.Xna.Framework.Graphics;

namespace ThreeDWorld
{
    public abstract class Game : IDisposable
    {
        public Game()
        {
            Camera = new Camera();
            SpriteBatch = new SpriteBatch(this);
            Sprite3DBatch = new Sprite3DBatch(this);
            Content = new ContentManager(this);
            SkyBox = new SkyBox(this);
        }

        public GraphicsDevice GraphicsDevice
        {
            get { return GraphicsDeviceManager.Current.GraphicsDevice; }
        }

        public Camera Camera
        {
            get;
            private set;
        }

        public SkyBox SkyBox
        {
            get;
            private set;
        }

        public SpriteBatch SpriteBatch
        {
            get;
            private set;
        }

        public Sprite3DBatch Sprite3DBatch
        {
            get;
            private set;
        }

        public ContentManager Content
        {
            get;
            private set;
        }

        protected abstract void Update(float elapsedTime, float totalTime);

        protected abstract void Draw(float elapsedTime, float totalTime);

        public void OnDraw(DrawEventArgs e)
        {
            try
            {
                Update((float)e.DeltaTime.TotalSeconds, (float)e.TotalTime.TotalSeconds);
                Draw((float)e.DeltaTime.TotalSeconds, (float)e.TotalTime.TotalSeconds);
            }
            catch (ObjectDisposedException)
            {
            }
            finally
            {
                e.InvalidateSurface();
            }
        }

        public void Dispose()
        {
            SpriteBatch.Dispose();
            Sprite3DBatch.Dispose();
            SkyBox.Dispose();
            Content.Unload();
        }
    }
}
